package com.cms.controller;

import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import com.cms.common.StringUtils;
import com.cms.constant.CmsConstant;
import com.cms.model.TrAdminUser;
import com.cms.model.TrBankInfo;
import com.cms.service.BankInfoService; 
import com.google.gson.Gson;

@Controller
public class BankInfoController extends BaseController {

	@Autowired
	private BankInfoService bankInfoService;
	 

	@RequestMapping("/bankInfo.do")
	public String enter(HttpServletRequest req, HttpServletResponse resp, ModelMap map) throws Exception {
		map.put("bankList", this.bankInfoService.getBankInfoList());
		return "bank/info/bankInfoMain.html";
	}
	
	@RequestMapping("/bankInfo/addBankInfo.do")
	public void addBankInfo(HttpServletRequest req, HttpServletResponse resp, ModelMap map) throws Exception {
		TrBankInfo info = new TrBankInfo();
		TrAdminUser tu = this.getLoginUser(req);
		info.setCreateUser(tu.getId());
		info.setModifyUser(tu.getId());
		Map<String, Object> param = new HashMap<String, Object>();
		try {
			this.export(req, "bankInfo", info);
			this.bankInfoService.saveBankInfo(info);
			param.put(CmsConstant.STATUS, CmsConstant.SUCCESS_STATUS);
		} catch (Exception e) {
			param.put(CmsConstant.STATUS, CmsConstant.FAIL_STATUS);
			param.put(CmsConstant.MSG, e.getMessage());
		}
		this.renderText(resp, new Gson().toJson(param));
	}
	
	
	@RequestMapping("/bankInfo/updateBankInfo.do")
	public void updateBankInfo(HttpServletRequest req, HttpServletResponse resp, ModelMap map) throws Exception
	{
		TrBankInfo info = new TrBankInfo();
		TrAdminUser tu = this.getLoginUser(req);
		info.setCreateUser(tu.getId());
		info.setModifyUser(tu.getId());
		Map<String, Object> param = new HashMap<String, Object>();
		try {
			this.export(req, "bankInfo", info);
			this.bankInfoService.updateBankInfo(info);
			param.put(CmsConstant.STATUS, CmsConstant.SUCCESS_STATUS);
		} catch (Exception e) {
			param.put(CmsConstant.STATUS, CmsConstant.FAIL_STATUS);
			param.put(CmsConstant.MSG, e.getMessage());
		}
		this.renderText(resp, new Gson().toJson(param));
	}
	
	
	@RequestMapping("/bankInfo/pageBankInfoList.do")
	public String pageBankInfoList(HttpServletRequest req, HttpServletResponse resp, ModelMap map) throws Exception {
		map.put("bankInfoList", this.bankInfoService.getBankInfoList());
		return "bank/info/bankInfoList.html";
	}
	
	@RequestMapping("/bankInfo/toRegBankInfo.do")
	public String toRegBankInfo(HttpServletRequest req, HttpServletResponse resp, ModelMap map) throws Exception {
		map.put("bankInfo", new TrBankInfo());
		map.put("action", "bankInfo/addBankInfo.do");
		return "bank/info/bankInfo.html";
	}
	
	@RequestMapping("/bankInfo/toEditBankInfo.do")
	public String toEditBankInfo(HttpServletRequest req, HttpServletResponse resp, ModelMap map) throws Exception {
		int bankId=StringUtils.str2Int(req.getParameter("bankId"));
		TrBankInfo info = new TrBankInfo();
		info.setId(bankId);
		map.put("bankInfo", this.bankInfoService.getBankInfo(info));
		map.put("action", "bankInfo/updateBankInfo.do");
		return "bank/info/bankInfo.html";
	}
}
